import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

/*
  数据模块化

  1. 商品列表
  2. 购物车
  3. 用信息
*/

export default new Vuex.Store({
  state: {
    count: 1024,
    isLogin: false,
  },
  mutations: {
    doLogin(state, payload) {
      state.isLogin = payload
    },
    add(state) {
      state.count += parseInt(Math.random() * 3);
    }
  },
  actions: {
    doLoginAsync({ commit }, payload) {
      return new Promise((resolve) => {
        setTimeout(() => {
          commit('doLogin', payload)
          resolve(true)
        }, 1e3 * 2)
      })
    },
    addAsync({ commit }, payload) {
      console.log(payload);
      setTimeout(() => {
        commit('add')
      }, 1e3)
    }
  },
  getters: { // computed
    money(state) {
      return `${state.count} 元`
    }
  },
  modules: {
  }
})
